package com.example.demo.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.user.service.IUCommentService;
import com.example.demo.user.entity.UComment;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 运营人员评价表; 前端控制器
 * </p>
 *
 * @author tao
 * @since 2024-10-29
 */
@RestController
@RequestMapping("/uComment")
    public class UCommentController {

    @Autowired
    private IUCommentService uCommentService;

    /**
    * 获取相关列表
     * @param current
     * @param size
     * @return
    */
    @GetMapping("/getUCommentList")
    public Result<?> getUCommentList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                            @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                            UComment uComment){

        Page<UComment> page = new Page<>(current,size);
        QueryWrapper<UComment> queryWrapper=new QueryWrapper<>(uComment);
        queryWrapper.orderByDesc("id");
        return Result.ok(uCommentService.page(page,queryWrapper));
    }

    /**
         * 根据ID查询信息
         */
    @GetMapping("/getUCommentById")
    public Result<?> getUCommentById(Integer id){
        return Result.ok(uCommentService.getById(id));
    }

    /**
         * 修改
         */
    @PutMapping("/updateUComment")
    @Transactional
    public Result<?> updateUComment(@RequestBody UComment uComment){
        return Result.ok(uCommentService.updateById(uComment));
    }

    /**
         * 新增
         */
    @PostMapping("/addUComment")
    public Result<?> addUComment(@RequestBody UComment uComment){
        return Result.ok(uCommentService.save(uComment));
    }

    /**
         * 删除
         */
    @DeleteMapping("/deleteUCommentById")
    @Transactional
    public Result<?> deleteUComment(Integer id){
        return Result.ok(uCommentService.removeById(id));
    }

    /**
     * 服务人员端获取评价统计
     */
    @GetMapping("/getUCommentResult")
    public Result<?> getUCommentResult(Integer id){
        QueryWrapper<UComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sum(comment_type=1) as goodCount,sum(comment_type=2) as badCount,count(*) as allCount");
        queryWrapper.eq("uid",id);
        return Result.ok(uCommentService.getOne(queryWrapper));
    }
}

